package org.soenea.doitall.domain.command;

import org.dsrg.soenea.domain.MapperException;
import org.dsrg.soenea.domain.command.CommandError;
import org.dsrg.soenea.domain.command.CommandException;
import org.dsrg.soenea.domain.command.DomainCommand;
import org.dsrg.soenea.domain.helper.Helper;
import org.soenea.doitall.domain.model.group.Group;
import org.soenea.doitall.domain.model.group.mapper.GroupInputMapper;
import org.soenea.doitall.domain.model.group.mapper.GroupOutputMapper;

public class EditGroupCommand extends DomainCommand {

	public EditGroupCommand(Helper helper) {
		super(helper);
	}

	@Override
	public void setUp() throws CommandException {
	}

	@Override
	public void process() throws CommandException {
		try {
			String name = helper.getString("name");
			String description = helper.getString("description");
			long id = (Long) helper.getAttribute("id");
			long version = helper.getLong("version");
			Group group = GroupInputMapper.find(id);
			group.setDescription(description);
			group.setName(name);
			group.setVersion(version);
			GroupOutputMapper.update(group);
			helper.setRequestAttribute("groups", GroupInputMapper.findAll());
		} catch (MapperException e) {
			throw new CommandException(e);
		}
	}

	@Override
	public void tearDown() throws CommandError {
	}

}
